<template>
    <div id="ds8">
        <Breadcrumb style="margin-bottom: 10px">
            <BreadcrumbItem to="/superAdmin">首页</BreadcrumbItem>
            <BreadcrumbItem>运营分析平台</BreadcrumbItem>
            <BreadcrumbItem>邀请好友详情</BreadcrumbItem>
        </Breadcrumb>
        <div style="padding-right: 20px">
            <Select v-model="type" style="width:200px;">
                <Option v-for="item in typeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select> &nbsp;
            <Input v-model="selectMobile" placeholder="查询手机号码" style="width: 150px"/>
            &nbsp;
            <DatePicker v-model="startTime" type="date" placeholder="选择开始时间" style="width: 150px"></DatePicker>
            至
            <DatePicker v-model="endTime" type="date" placeholder="选择结束时间" style="width: 150px"></DatePicker>
            &nbsp;
            <Button type="info" @click="search()">查询</Button>
            <Button type="info" @click="handleDownload()" style="float: right">下载报表</Button>
        </div>


        <div style="margin-top: 10px">
            <Table :maxWidth="1000" :minWidth="10" stripe :columns="columns" :data="data"
                   v-if="data&&data.length>0"></Table>

            <Page :total="pageTotal" :current="curPage" @on-change="pageChange"
                  style="display: inline-block;padding-top: 10px;padding-bottom: 10px" show-elevator
                  v-if="data&&data.length>0"/>
            <div style="padding-left: 10px;position:relative ;display: inline-block;height: 52px;line-height: 52px;top: -12px">
                总计 {{pageTotal}} 条数据
            </div>
        </div>
        <div id="loading" v-if="loading">
            <div class="spinner">
                <div class="double-bounce1"></div>
                <div class="double-bounce2"></div>
            </div>
        </div>


    </div>
</template>
<script type="text/ecmascript-6">
    import sessionUtils from '../../model/sessionUtils';
    import ipServer from '../../model/ip';
    import Axios from 'axios';
    export default{
        data(){
            return {
                type: "",
                typeList: [
                    {
                        value: '1',
                        label: '司机'
                    },
                    {
                        value: '2',
                        label: '配件商'
                    },
                    {
                        value: '3',
                        label: '修理'
                    },
                    {
                        value: '4',
                        label: '补胎'
                    },
                    {
                        value: '5',
                        label: '审车'
                    },
                    {
                        value: '6',
                        label: '加油'
                    },
                    {
                        value: '7',
                        label: '吊车'
                    }
                ],
                startTime: "",
                endTime: "",
                selectMobile: "",
                loading: false,
                pageTotal: 0,
                curPage: 0,
                columns: [
                    {
                        title: '邀请时间',
                        key: 'inviteTime1'
                    },
                    {
                        title: '注册时间',
                        key: 'registerTime1'
                    },
                    {
                        title: '邀请人角色',
                        key: 'userType1'
                    },
                    {
                        title: '邀请人手机号',
                        key: 'mobile'
                    }, {
                        title: '邀请人等级',
                        key: 'currentLevel'
                    },
                    {
                        title: '被邀请人角色',
                        key: 'toUserType1'
                    },
                    {
                        title: '被邀请人手机号',
                        key: 'toMobile'
                    }
                ],
                data: []
            }
        },
        mounted(){
            this.getData();
        },
        methods: {
            search(){
                var param = new URLSearchParams();
                param.append('mobile', sessionUtils.getSession("mobile"));
                param.append('token', sessionUtils.getSession("token"));
                param.append('inviteUserType', this.type);
                if (this.startTime) {
                    param.append('startYearMonthDay', sessionUtils.DateToTime(this.startTime) + "");
                }
                if (this.endTime) {
                    param.append('endYearMonthDay', sessionUtils.DateToTime(this.endTime) + "");
                }
                if (this.selectMobile) {
                    param.append('inviteUserMobile', this.selectMobile + "");
                }
                this.loading = true;
                Axios.post(ipServer + "analysis/inviteFriendsDetail", param)
                        .then(res => {

                            if (res.data.code == 0) {

                                sessionUtils.setSession("token", res.data.token);
                                for (var i = 0; i < res.data.data.result.length; i++) {
                                    res.data.data.result[i].inviteTime1 = sessionUtils.timestampToTime(res.data.data.result[i].inviteTime);

                                    if (res.data.data.result[i].registerTime) {
                                        res.data.data.result[i].registerTime1 = sessionUtils.timestampToTime(res.data.data.result[i].registerTime);
                                    } else {
                                        res.data.data.result[i].registerTime1 = "未注册";
                                    }

                                    if (res.data.data.result[i].toUserType == "1") {
                                        res.data.data.result[i].toUserType1 = "司机";
                                    }
                                    if (res.data.data.result[i].toUserType == "2") {
                                        res.data.data.result[i].toUserType1 = "配件商";
                                    }
                                    if (res.data.data.result[i].toUserType == "3") {
                                        res.data.data.result[i].toUserType1 = "修理";
                                    }
                                    if (res.data.data.result[i].toUserType == "4") {
                                        res.data.data.result[i].toUserType1 = "补胎";
                                    }
                                    if (res.data.data.result[i].toUserType == "5") {
                                        res.data.data.result[i].toUserType1 = "审车";
                                    }
                                    if (res.data.data.result[i].toUserType == "6") {
                                        res.data.data.result[i].toUserType1 = "加油";
                                    }
                                    if (res.data.data.result[i].toUserType == "7") {
                                        res.data.data.result[i].toUserType1 = "吊车";
                                    }
                                    if (res.data.data.result[i].isDownLoad == 0) {
                                        res.data.data.result[i].toUserType1 = "未注册";
                                    }


                                    if (res.data.data.result[i].userType == "1") {
                                        res.data.data.result[i].userType1 = "司机";
                                    }
                                    if (res.data.data.result[i].userType == "2") {
                                        res.data.data.result[i].userType1 = "配件商";
                                    }
                                    if (res.data.data.result[i].userType == "3") {
                                        res.data.data.result[i].userType1 = "修理";
                                    }
                                    if (res.data.data.result[i].userType == "4") {
                                        res.data.data.result[i].userType1 = "补胎";
                                    }
                                    if (res.data.data.result[i].userType == "5") {
                                        res.data.data.result[i].userType1 = "审车";
                                    }
                                    if (res.data.data.result[i].userType == "6") {
                                        res.data.data.result[i].userType1 = "加油";
                                    }
                                    if (res.data.data.result[i].userType == "7") {
                                        res.data.data.result[i].userType1 = "吊车";
                                    }
                                }
                                this.data = res.data.data.result;
                                this.pageTotal = res.data.data.dataTotal;
                                this.curPage = res.data.data.curPage;
                                if (this.data.length == 0)
                                    this.$Message.info("查询数据不存在!");
                            } else if (res.data.code == 3) {
                                sessionUtils.clear();
                                this.$Message.info(res.data.msg);
                                this.$router.push("/login");

                            } else {
                                this.$Message.info(res.data.msg);
                            }
                            this.loading = false;
                        })
                        .catch(err=> {
                            this.loading = false;
                            this.$Message.info(err);
                        });
            },
            pageChange(page){
                this.getData(page);
            },
            getData(page){
                var param = new URLSearchParams();
                param.append('mobile', sessionUtils.getSession("mobile"));
                param.append('token', sessionUtils.getSession("token"));
                param.append('inviteUserType', this.type);
                if (page) {
                    param.append('pageId', page + "");
                }
                if (this.startTime) {
                    param.append('startYearMonthDay', sessionUtils.DateToTime(this.startTime) + "");
                }
                if (this.endTime) {
                    param.append('endYearMonthDay', sessionUtils.DateToTime(this.endTime) + "");
                }
                if (this.selectMobile) {
                    param.append('inviteUserMobile', this.selectMobile + "");
                }

                this.loading = true;
                Axios.post(ipServer + "analysis/inviteFriendsDetail", param)
                        .then(res => {
                            if (res.data.code == 0) {
                                sessionUtils.setSession("token", res.data.token);
                                for (var i = 0; i < res.data.data.result.length; i++) {
                                    res.data.data.result[i].inviteTime1 = sessionUtils.timestampToTime(res.data.data.result[i].inviteTime);

                                    if (res.data.data.result[i].registerTime) {
                                        res.data.data.result[i].registerTime1 = sessionUtils.timestampToTime(res.data.data.result[i].registerTime);
                                    } else {
                                        res.data.data.result[i].registerTime1 = "未注册";
                                    }

                                    if (res.data.data.result[i].toUserType == "1") {
                                        res.data.data.result[i].toUserType1 = "司机";
                                    }
                                    if (res.data.data.result[i].toUserType == "2") {
                                        res.data.data.result[i].toUserType1 = "配件商";
                                    }
                                    if (res.data.data.result[i].toUserType == "3") {
                                        res.data.data.result[i].toUserType1 = "修理";
                                    }
                                    if (res.data.data.result[i].toUserType == "4") {
                                        res.data.data.result[i].toUserType1 = "补胎";
                                    }
                                    if (res.data.data.result[i].toUserType == "5") {
                                        res.data.data.result[i].toUserType1 = "审车";
                                    }
                                    if (res.data.data.result[i].toUserType == "6") {
                                        res.data.data.result[i].toUserType1 = "加油";
                                    }
                                    if (res.data.data.result[i].toUserType == "7") {
                                        res.data.data.result[i].toUserType1 = "吊车";
                                    }
                                    if (res.data.data.result[i].isDownLoad == 0) {
                                        res.data.data.result[i].toUserType1 = "未注册";
                                    }
                                    if (res.data.data.result[i].userType == "1") {
                                        res.data.data.result[i].userType1 = "司机";
                                    }
                                    if (res.data.data.result[i].userType == "2") {
                                        res.data.data.result[i].userType1 = "配件商";
                                    }
                                    if (res.data.data.result[i].userType == "3") {
                                        res.data.data.result[i].userType1 = "修理";
                                    }
                                    if (res.data.data.result[i].userType == "4") {
                                        res.data.data.result[i].userType1 = "补胎";
                                    }
                                    if (res.data.data.result[i].userType == "5") {
                                        res.data.data.result[i].userType1 = "审车";
                                    }
                                    if (res.data.data.result[i].userType == "6") {
                                        res.data.data.result[i].userType1 = "加油";
                                    }
                                    if (res.data.data.result[i].userType == "7") {
                                        res.data.data.result[i].userType1 = "吊车";
                                    }
                                }
                                this.data = res.data.data.result;
                                this.pageTotal = res.data.data.dataTotal;
                                this.curPage = res.data.data.curPage;
                            } else if (res.data.code == 3) {
                                sessionUtils.clear();
                                this.$router.push("/login");
                                this.$Message.info(res.data.msg);
                            } else {
                                this.$Message.info(res.data.msg);
                            }
                            this.loading = false;
                        })
                        .catch(err=> {
                            this.loading = false;
                            this.$Message.info(err);
                        });
            }, handleDownload() {
                this.loading = true;
                const list = this.data;
                require.ensure([], () => {
                    const {export_json_to_excel} = require('../../assets/js/Export2Excel') //这个地址和页面的位置相关，这个地址是Export2Excel.js相对于页面的相对位置
                    const tHeader = ["邀请时间", "注册时间", "邀请人角色", "邀请人手机号", "邀请人等级", "被邀请人角色", "被邀请人手机号"]; //这个是表头名称 可以是iveiw表格中表头属性的title的数组
                    const filterVal = ["inviteTime1", "registerTime1", "userType1", "mobile", "currentLevel", "toUserType1", "toMobile"]; //与表格数据配合 可以是iview表格中的key的数组
                    const data = this.formatJson(filterVal, list)
                    export_json_to_excel(tHeader, data, '邀请详情') //列表excel  这个是导出表单的名称
                    this.loading = false
                })
            },
            formatJson(filterVal, jsonData) {
                return jsonData.map(v => filterVal.map(j => v[j]))
            }
        }
    }
</script>


<style scoped>
    @import "../../assets/css/loading.css";

</style>